#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{

    struct sockaddr_in ser_sock;
    socklen_t ser_sock_len;
    char buf[1024];
    int buflen = sizeof(buf);
    char serverip[] = "172.27.151.152";
    // char serverip[] = "127.0.0.1";
    char PORT[] = "3333";

    int csock = socket(AF_INET, SOCK_STREAM, 0);
    ser_sock_len = sizeof(ser_sock);

    ser_sock.sin_family = AF_INET;
    ser_sock.sin_port = htons(atoi(PORT));
    ser_sock.sin_addr.s_addr = inet_addr(serverip);

    if(connect(csock, (struct sockaddr *)&ser_sock, ser_sock_len) != 0 ){
        perror("connect is fail\n");
        exit(0);
    }
    memset(&buf, 0, sizeof(buf));
    recv(csock, &buf, sizeof(buf), 0);
    printf(" %s \n", buf);
    for (size_t i = 0; i < 3; i++)
    {
        bzero(&buf, buflen);
        sprintf(buf, "%s %ld", "clinet send message", i);
        send(csock, &buf, buflen, 0);
        bzero(&buf, buflen);
        recv(csock, &buf, buflen, 0);
        printf("从服务器发送的信息: %s \n", buf);
    }

    close(csock);
    

    return 0;
}